package com.example.studentgrades.controller;


import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.studentgrades.dao.ScoreDAO;
import com.example.studentgrades.model.Score;

@WebServlet("/score/*")
public class ScoreServlet extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();

        if (pathInfo.equals("/add")) {
            addScore(request, response);
        } else if (pathInfo.equals("/update")) {
            updateScore(request, response);
        } else if (pathInfo.equals("/delete")) {
            deleteScore(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();

        if (pathInfo.equals("/list")) {
            listScores(request, response);
        }
    }

    private void addScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        String subject = request.getParameter("subject");
        int scoreValue = Integer.parseInt(request.getParameter("score"));

        Score score = new Score();
        score.setStudentId(studentId);
        score.setSubject(subject);
        score.setScore(scoreValue);

        try (Connection connection = getConnection()) {
            ScoreDAO scoreDAO = new ScoreDAO(connection);
            scoreDAO.addScore(score);
            redirectToPage(response, "score_list.jsp"); // 成绩添加成功后跳转到成绩列表页面
        } catch (SQLException e) {
            handleSQLException(response, e, "score_add.jsp"); // 添加失败返回添加页面
        }
    }

    private void updateScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int scoreId = Integer.parseInt(request.getParameter("scoreId"));
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        String subject = request.getParameter("subject");
        int scoreValue = Integer.parseInt(request.getParameter("score"));

        Score score = new Score();
        score.setScoreId(scoreId);
        score.setStudentId(studentId);
        score.setSubject(subject);
        score.setScore(scoreValue);

        try (Connection connection = getConnection()) {
            ScoreDAO scoreDAO = new ScoreDAO(connection);
            scoreDAO.updateScore(score);
            redirectToPage(response, "score_list.jsp"); // 成绩更新成功后跳转到成绩列表页面
        } catch (SQLException e) {
            handleSQLException(response, e, "score_update.jsp"); // 更新失败返回更新页面
        }
    }

    private void deleteScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int scoreId = Integer.parseInt(request.getParameter("scoreId"));

        try (Connection connection = getConnection()) {
            ScoreDAO scoreDAO = new ScoreDAO(connection);
            scoreDAO.deleteScore(scoreId);
            redirectToPage(response, "score_list.jsp"); // 成绩删除成功后跳转到成绩列表页面
        } catch (SQLException e) {
            handleSQLException(response, e, "score_list.jsp"); // 删除失败返回列表页面
        }
    }

    private void listScores(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try (Connection connection = getConnection()) {
            ScoreDAO scoreDAO = new ScoreDAO(connection);
            List<Score> scores = scoreDAO.getAllScores();
            request.setAttribute("scores", scores);
            forwardToPage(request, response, "score_list.jsp"); // 转发到成绩列表页面
        } catch (SQLException e) {
            handleSQLException(response, e, "error.jsp"); // 查询失败返回错误页面
        }
    }

}

